package com.shxzz.easy12306.services.ticket.common.enums;

import cn.hutool.core.text.CharSequenceUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * canal 信息消费执行策略标记枚举类
 * @author shxzz
 * @create 2024-03-18-23:15
 */
@Getter
public enum CanalExecuteStrategyMark {
    /**
     * 座位表
     */
    T_SEAT("t_seat", null),

    /**
     * 订单表
     */
    T_ORDER("t_order", "^t_order_([0-9]+|1[0-6])");

    /**
     * 逻辑表
     */
    private final String actualTable;


    /**
     * 泛匹配真实表
     */
    private final String patternMatchTable;

    /**
     * 正则表达式模板
     */
    private final Pattern pattern;

    CanalExecuteStrategyMark(String actualTable, String patternMatchTable) {
        this.actualTable = actualTable;
        this.patternMatchTable = patternMatchTable;
        this.pattern = CharSequenceUtil.isNotBlank(patternMatchTable) ? Pattern.compile(patternMatchTable) : null;
    }

    /**
     * 是否是泛匹配表
     */
    public static boolean isPatternMatch(String tableName) {
        return Arrays.stream(CanalExecuteStrategyMark.values())
                .filter(mark -> mark.getPattern() != null)
                .anyMatch(mark -> mark.getPattern().matcher(tableName).matches());
    }
}
